package com.vincent.filepicker;

import com.vincent.filepicker.adapter.FolderListAdapter;
import com.vincent.filepicker.filter.entity.Directory;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.ListContainer;
import ohos.agp.utils.LayoutAlignment;
import ohos.agp.window.dialog.PopupDialog;
import ohos.app.Context;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Vincent Woo
 * Date: 2018/2/27
 * Time: 13:43
 */

public class FolderListHelper {
    private Context mContext;
    private PopupDialog mPopupWindow;
    private Component mContentView;
    private ListContainer rv_folder;
    private FolderListAdapter mAdapter;

    public void initFolderListView(Context ctx) {
        mContext = ctx;
        mAdapter = new FolderListAdapter(mContext, new ArrayList<Directory>());
    }

    public void setFolderListListener(FolderListAdapter.FolderListListener listener) {
        mAdapter.setListener(listener);
    }

    public void fillData(List<Directory> list) {
        mAdapter.refresh(list);
    }

    public void toggle(Component anchor) {

        if (null != mPopupWindow && mPopupWindow.isShowing()) {
            mPopupWindow.hide();
            return;
        }
        mContentView = LayoutScatter.getInstance(mContext)
                .parse(ResourceTable.Layout_vw_layout_folder_list, null, false);
        rv_folder = (ListContainer) mContentView.findComponentById(ResourceTable.Id_rv_folder);

        rv_folder.setItemProvider(mAdapter);
        mPopupWindow = new PopupDialog(mContext, anchor);
        mPopupWindow.setAlignment(LayoutAlignment.HORIZONTAL_CENTER | LayoutAlignment.TOP);
        mPopupWindow.setSize(ComponentContainer.LayoutConfig.MATCH_CONTENT, ComponentContainer.LayoutConfig.MATCH_CONTENT);
        mPopupWindow.setCustomComponent(mContentView);
        mPopupWindow.setAutoClosable(true);
        mPopupWindow.show();
        mAdapter.notifyDataChanged();
    }
}
